package com.yks.demo.lvcr.democlient.service.impl;

import com.yks.demo.lvcr.democlient.entity.DemoClass;
import com.yks.demo.lvcr.democlient.entity.DemoUser;
import com.yks.demo.lvcr.democlient.mapper.IDemoClassMapper;
import com.yks.demo.lvcr.democlient.mapper.IDemoUserMapper;
import com.yks.demo.lvcr.democlient.service.IDbService;
import com.yks.demo.lvcr.democlient.service.IDemoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;

@Service
public class DemoUserServiceImpl implements IDemoUserService {
    @Autowired
    private IDemoUserMapper demoUserMapper;

    @Autowired
    private IDemoClassMapper demoClassMapper;

    @Autowired
    private IDbService dbService;

    @Override
    public int addUser(DemoUser demoUser) {
        return demoUserMapper.insert(demoUser);
    }

    @Override
    public int updateUser(DemoUser demoUser) {
        return demoUserMapper.update(demoUser);
    }

    @Override
    @Transactional
    public int testWaitTimeOut(DemoUser demoUser) {
        demoUserMapper.insert(demoUser);
        DemoUser updateUser = new DemoUser();
        updateUser.setId(1);
        updateUser.setUserName(demoUser.getUserName());
//        int i=1/0;
        try {
            Thread.sleep(Long.valueOf(60 * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return demoUserMapper.update(updateUser);
    }

    @Override
    public int testMaxIdleTimeOut(DemoUser demoUser) {
        demoUserMapper.insert(demoUser);
        DemoUser updateUser = new DemoUser();
        updateUser.setId(1);
        updateUser.setUserName(demoUser.getUserName());
        return demoUserMapper.update(updateUser);
    }

    @Override
    @Transactional
    public int testTransactionParent(DemoUser demoUser) {
        //return demoUserMapper.insert(demoUser);
        int rtn = demoUserMapper.insert(demoUser);
        DemoClass demoClass = new DemoClass();
        demoClass.setClassName("className1");
        try {
            testTransactionChild(demoClass);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rtn;
    }   

    @Override
    //如果当前存在事务，则挂起当前事务并且开启一个新事物继续执行，
    // 新事物执行完毕之后，然后在缓刑之前挂起的事务，如果当前不存在事务的话，则开启一个新事物。
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void   testTransactionChild(DemoClass demoClass) {
        int rtn = demoClassMapper.insert(demoClass);
        throw new RuntimeException("this is a Excetion");
    }

    @Override
    public void testTransaction(DemoUser demoUser) {
        int rtn = demoUserMapper.insert(demoUser);
        DemoClass demoClass = new DemoClass();
        demoClass.setClassName("test1");
        dbService.testTransactionTwo(demoClass);
    }

    @Override
    @Transactional
    public void testTransactionTwo(DemoClass demoClass) {

        demoClassMapper.insert(demoClass);

       // throw new RuntimeException("this is a Exception");
    }


}
